﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ServiceOfSansar.Models;

namespace ServiceOfSansar
{
    [ServiceContract(Namespace = "Sansar")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Report
    {


        #region Database
        private SansarEntities db;
        public Report()
        {
            db = new SansarEntities();
        }
        ~Report()
        {
            db.Dispose();
        }
        #endregion
        [OperationContract]
        public TransferReceipt TransferInvoice(long jrno)
        {
            TransferReceipt result = new TransferReceipt();
            var trans = db.Transactions.Where(w => w.JrNo == jrno).OrderBy(o=>o.TxnDate).ToList();
            if (trans.Count() > 0)
            {
                Transaction t=trans[0];
                long cno = t.ContractNo ?? 0;
                var cont = db.Contracts.FirstOrDefault(w => w.ContractNo == cno);
                if(cont!=null)
                {
                    
                    var cust = db.Customers.FirstOrDefault(w => w.CustNo == cont.CustNo);
                    if (cust != null)
                    {
                        result.CustomerName = cust.CustName;
                        result.ID="";//cont
                        result.JrNo = t.JrNo;
                        result.UserName = (db.Users.FirstOrDefault(w => w.UserId == t.UserNo) ?? (new User() { Name="" })).Name;
                        result.Transactions = trans;
                        result.Date = t.TxnDate.ToString("yyyy.MM.dd"); 
                    }
                }
            }

            return result;
        }
    }
}
